package com.netsdk.demo.customize;

import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.callback.impl.DefaultDisconnectCallback;
import com.netsdk.lib.callback.impl.DefaultHaveReconnectCallBack;
import com.netsdk.lib.enumeration.*;
import com.netsdk.lib.structure.*;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.netsdk.lib.Utils.getOsPrefix;

public class SoftTourPointDemo {

    // SDk对象初始化
    public static final NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
    public static final NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE;


    // 判断是否初始化
    private static boolean bInit = false;
    // 判断log是否打开
    private static boolean bLogOpen = false;
    // 设备信息
    private NetSDKLib.NET_DEVICEINFO_Ex deviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
    // 登录句柄
    private NetSDKLib.LLong loginHandle = new NetSDKLib.LLong(0);

    // 回调函数需要是静态的，防止被系统回收
    // 断线回调
    private static NetSDKLib.fDisConnect disConnectCB = DefaultDisconnectCallback.getINSTANCE();
    // 重连回调
    private static NetSDKLib.fHaveReConnect haveReConnectCB = DefaultHaveReconnectCallBack.getINSTANCE();

    //订阅句柄
    NetSDKLib.LLong lAttachHandle = new NetSDKLib.LLong(0);
    // 编码格式
    public static String encode;

    static {
        String osPrefix = getOsPrefix();
        if (osPrefix.toLowerCase().startsWith("win32-amd64")) {
            encode = "GBK";
        } else if (osPrefix.toLowerCase().startsWith("linux-amd64")) {
            encode = "UTF-8";
        }
    }

    /**
     * 按照指定格式，获取当前时间
     */
    public static String getDate() {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDate.format(new Date()).replaceAll("[^0-9]", "-");
    }

    /**
     * 初始化SDK库
     */
    public static boolean initSdk() {
        bInit = netsdk.CLIENT_Init(disConnectCB, null);// 进程启动时，初始化一次
        if (!bInit) {
            System.out.println("Initialize SDK failed");
            return false;
        }
        // 配置日志
        enableLog();
        // 设置断线重连回调接口, 此操作为可选操作，但建议用户进行设置
        netsdk.CLIENT_SetAutoReconnect(haveReConnectCB, null);
        // 设置登录超时时间和尝试次数，可选
        // 登录请求响应超时时间设置为3S
        int waitTime = 3000; //单位为ms
        // 登录时尝试建立链接 1 次
        int tryTimes = 1;
        // 设置连接设备超时时间和尝试次数
        netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
        // 设置更多网络参数， NET_PARAM 的nWaittime ， nConnectTryNum 成员与 CLIENT_SetConnectTime
        // 接口设置的登录设备超时时间和尝试次数意义相同,可选
        NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
        // 登录时尝试建立链接的超时时间
        netParam.nConnectTime = 10000;
        // 设置子连接的超时时间
        netParam.nGetConnInfoTime = 3000;
        //设置登陆网络环境
        netsdk.CLIENT_SetNetworkParam(netParam);
        return true;
    }

    /**
     * 打开 sdk log
     */
    private static void enableLog() {
        // SDK全局日志打印信息
        NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
        //设置日志路径
        File path = new File("sdklog/");
        //判断日志路径是否存在，若不存在则创建
        if (!path.exists()){
            path.mkdir();
        }
        // 这里的log保存地址依据实际情况自己调整
        String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + "sdklog" + getDate() + ".log";
        //日志输出策略,0:输出到文件(默认); 1:输出到窗口,
        setLog.nPrintStrategy = 0;
        //是否重设日志路径, 取值0否 ,1是
        setLog.bSetFilePath = 1;
        //日志路径(默认"./sdk_log/sdk_log.log")
        byte[] szLogFilePath = setLog.szLogFilePath;
        //自定义log保存地址，将数据logPath数据copy到LOG_SET_PRINT_INFO-->szLogFilePath变量中
        System.arraycopy(logPath.getBytes(), 0, szLogFilePath, 0, logPath.getBytes().length);
        //是否重设日志打印输出策略 取值0否 ,1是
        setLog.bSetPrintStrategy = 1;
        // 打开日志功能
        bLogOpen = netsdk.CLIENT_LogOpen(setLog);
        if (!bLogOpen){
            System.err.println("Failed to open NetSDK log "+ ToolKits.getErrorCode());
        }else {
            System.out.println("Success to open NetSDK log ");
        }
    }

    /**
     * 高安全登录
     */
    public void loginWithHighLevel() {
        // 输入结构体参数
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstlnParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() {
            {
                // IP
                szIP = m_strIpAddr.getBytes();
                // 端口
                nPort = m_nPort;
                // 用户名
                szUserName = m_strUser.getBytes();
                // 密码
                szPassword = m_strPassword.getBytes();
            }
        };
        // 登录的输出结构体参数
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
        // 高安全级别登陆
        loginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstlnParam, pstOutParam);
        if (loginHandle.longValue() == 0) {  //登陆失败
            System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIpAddr, m_nPort, ToolKits.getErrorCode());

        } else {
            //登陆成功
            // 获取设备信息
            deviceInfo = pstOutParam.stuDeviceInfo;
            System.out.printf("Login Success Device Address[%s] 设备包含[%d]个通道 \n",m_strIpAddr,deviceInfo.byChanNum);
        }
    }

    /**
     * 退出
     */
    public void logOut() {
        //判断是否已登录
        if (loginHandle.longValue() != 0) {
            netsdk.CLIENT_Logout(loginHandle);
            System.out.println("LogOut Success");
        }
    }

    /**
     * 清理sdk环境并退出
     */
    public static void cleanAndExit() {
        //判断log是否打开
        if (bLogOpen) {
            // 关闭sdk日志打印
            netsdk.CLIENT_LogClose();
        }
        // 进程关闭时，调用一次
        netsdk.CLIENT_Cleanup();
        System.exit(0);
    }

    /**
     * 初始化测试
     */
    public void initTest() {
        initSdk();

        this.loginWithHighLevel();
    }

    /**
     * 结束测试
     */
    public void endTest() {
        System.out.println("End Test");
        this.logOut(); // 登出设备
        System.out.println("See You...");
        cleanAndExit(); // 清理资源并退出
    }

    public void runTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();
        menu.addItem(new CaseMenu.Item(this, "水平旋转设置配置", "getAutoPan"));
        menu.addItem(new CaseMenu.Item(this, "新增软巡航预设点", "addSoftTourPoint"));
        menu.addItem(new CaseMenu.Item(this, "清除软巡航预设点", "removeSoftTourPoint"));
        menu.addItem(new CaseMenu.Item(this, "获取摄像机的状态开发", "getPtzMovement"));
        menu.addItem(new CaseMenu.Item(this, "补光灯设置", "setAudioOutVolume"));
        menu.addItem(new CaseMenu.Item(this, "巡航点设置", "setPreset"));
        menu.run();
    }


    /**
     * 水平旋转设置配置
     * @return
     */
    public boolean getAutoPan(){
        NET_CFG_AUTO_PAN_INFO config = new NET_CFG_AUTO_PAN_INFO();

        Pointer pointer = new Memory(config.size());
        pointer.clear(config.size());
        ToolKits.SetStructDataToPointer(config, pointer, 0);

        boolean getRet = netsdk.CLIENT_GetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_AUTO_PAN, 0,
                pointer, config.size(), 5000, null);
        ToolKits.GetPointerData(pointer, config);

        if (!getRet) {
            System.err.printf("getAutoPan Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError());
            return false;
        }else {
            System.out.println("nPanSpeed--->" + config.nPanSpeed);
            System.out.println("getAutoPan succeed");
        }

        config.nPanSpeed = 1;
        ToolKits.SetStructDataToPointer(config, pointer, 0);
        boolean setRet = netsdk.CLIENT_SetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_AUTO_PAN, 0,
                pointer, config.size(), 5000, new IntByReference(0), null);

        if (!setRet) {
            System.err.printf("setAutoPan Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError());
            return false;
        }else {
            System.out.println("setAutoPan succeed");
        }
        return true;
    }


    /**
     * 获取摄像机开发状态
     * @return
     */
    public boolean getPtzMovement(){
        NET_CFG_PTZ_MOVEMENT_INFO info = new NET_CFG_PTZ_MOVEMENT_INFO();
        Pointer pointer = new Memory(info.size());
        pointer.clear(info.size());
        ToolKits.SetStructDataToPointer(info, pointer, 0);

        boolean bRet = netsdk.CLIENT_GetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_PTZ_MOVEMENT, 0,
                pointer, info.size(), 5000, null);
        ToolKits.GetPointerData(pointer, info);

        if (!bRet) {
            System.err.printf("getPtzMovement Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError());
            return false;
        }else {
            System.out.println("获取摄像机开发状态");
            System.out.println("云台功能为" + EM_PTZ_FUNCTION.getNoteByValue(info.emFunction));
            System.out.println("任务名称为" + EM_PTZ_TASK_NAME.getNoteByValue(info.emTaskName));
            System.out.println("getPtzMovement succeed");
            return true;
        }
    }

    public boolean addSoftTourPoint(){

        NET_IN_ADD_SOFT_TOUR_POINT_INFO pstInParam = new NET_IN_ADD_SOFT_TOUR_POINT_INFO();
        NET_OUT_ADD_SOFT_TOUR_POINT_INFO pstOutParam = new NET_OUT_ADD_SOFT_TOUR_POINT_INFO();

        pstInParam.nChannel = 0;//云台通道号
        pstInParam.nIndex = 1;//巡航线路编号
        pstInParam.nPoint = 1;//线路上点的序号，从0开始, 如果为 -1, 追加到线路上最后位置，否则为指定序号
        pstInParam.nPresetIndex = 1;//预置点编号,编号从1开始
        pstInParam.nDuration = 15;//在该预置点停留时间,时间单位为秒
        pstInParam.fSpeed = 0.5f;//转动到预置点的速度, 归一化到0~1
        System.out.println("云台通道号-- " + pstInParam.nChannel);
        System.out.println("巡航线路编号-- " + pstInParam.nIndex);
        System.out.println("线路上点的序号-- " + pstInParam.nPoint);
        System.out.println("预置点编号-- " + pstInParam.nPresetIndex);
        System.out.println("在该预置点停留时间,时间单位为秒-- " + pstInParam.nDuration);
        System.out.println("转动到预置点的速度, 归一化到0~1-- " + pstInParam.fSpeed);
        pstInParam.write();
        pstOutParam.write();
        boolean bRet = netsdk.CLIENT_AddSoftTourPoint(loginHandle, pstInParam.getPointer(), pstOutParam.getPointer(), 3000);
        pstInParam.read();
        pstOutParam.read();
        if (!bRet) {
            System.err.printf("addSoftTourPoint Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError());
            return false;
        }else {
            System.out.println("CLIENT_AddSoftTourPoint succeed");
            return true;
        }
    }

    public boolean removeSoftTourPoint(){

        NET_IN_REMOVE_SOFT_TOUR_POINT_INFO pstInParam = new NET_IN_REMOVE_SOFT_TOUR_POINT_INFO();
        NET_OUT_REMOVE_SOFT_TOUR_POINT_INFO pstOutParam = new NET_OUT_REMOVE_SOFT_TOUR_POINT_INFO();

        pstInParam.nChannel = 0;//云台通道号
        pstInParam.nIndex = 1;//巡航线路编号
        pstInParam.nPoint = 1;//线路上点的序号，从0开始, 如果为 -1, 追加到线路上最后位置，否则为指定序号
        pstInParam.nPresetIndex = 1;//预置点编号,编号从1开始
        pstInParam.write();
        pstOutParam.write();
        boolean bRet = netsdk.CLIENT_RemoveTourPoint(loginHandle, pstInParam.getPointer(), pstOutParam.getPointer(), 3000);
        pstInParam.read();
        pstOutParam.read();
        if (!bRet) {
            System.err.printf("removeSoftTourPoint Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError());
            return false;
        }else {
            System.out.println("CLIENT_RemoveTourPoint succeed");
            return true;
        }
    }

    /**
     * 补光灯设置
     */
    public void setAudioOutVolume() {


        NET_IN_LIGHTINGCONTROL_CAPS stIn = new NET_IN_LIGHTINGCONTROL_CAPS();
        NET_OUT_LIGHTINGCONTROL_CAPS stOut = new NET_OUT_LIGHTINGCONTROL_CAPS();

        stIn.nChannel = 0;

        stIn.write();
        stOut.write();

        int nType = GetDevCaps_Type.NET_LIGHTINGCONTROL_CAPS.getType();

        boolean bRet = netsdk.CLIENT_GetDevCaps(loginHandle, nType, stIn.getPointer(), stOut.getPointer(), 3000);

        stIn.read();
        stOut.read();
        if (!bRet) {
            System.err.println("获取能力错误:" + ENUMERROR.getErrorMessage());
            return;
        } else {
            System.out.println("获取能力成功");
        }

        System.out.println("emConfigVersion-->" + stOut.emConfigVersion);
        if(stOut.emConfigVersion == 1){
            //使用Lighting配置，默认
            NET_VIDEOIN_LIGHTING_INFO config = new NET_VIDEOIN_LIGHTING_INFO();//补光灯参数
            Pointer pointer = new Memory(config.size());
            pointer.clear(config.size());
            ToolKits.SetStructDataToPointer(config, pointer, 0);
            int nChannelID = 0;// 通道号
            boolean resultIn = netsdk.CLIENT_GetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_VIDEOIN_LIGHTING, nChannelID,
                    pointer, config.size(), 5000, null);

            if (!resultIn) {
                System.err.println("获取补光灯配置失败:" + ENUMERROR.getErrorMessage());
                return;
            } else {
                System.out.println("获取补光灯配置成功");
            }
            ToolKits.GetPointerData(pointer, config);
            System.out.println("lightMode" +config.emLightMode);

            config.emLightMode = 1; //灯光模式
            config.nCorrection = 1; //灯光补偿值，倍率优先时有效0-4
            config.nSensitive = 2; //灯光灵敏度，倍率优先时有效，0-5，默认为3
            config.nNearLight = 30;//近光灯亮度0-100
            config.nFarLight = 30;//远光灯亮度0-100
            boolean resultOut = netsdk.CLIENT_SetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_VIDEOIN_LIGHTING, nChannelID,
                    pointer, config.size(),5000, new IntByReference(0), null);
            if (!resultOut) {
                System.err.println("设置补光灯配置失败:" + ENUMERROR.getErrorMessage());
            } else {
                System.out.println("设置补光灯配置成功");
            }
        }else if(stOut.emConfigVersion == 2){
            //使用Lighting_V2配置
            CFG_LIGHTING_V2_INFO config = new CFG_LIGHTING_V2_INFO();
            int channel = 0;// 通道号
            String command = EM_NEW_CONFIG.CFG_CMD_LIGHTING_V2.getValue();
            Pointer pointer = new Memory(config.size());
            pointer.clear(config.size());
            ToolKits.SetStructDataToPointer(config, pointer, 0);
            if (ToolKits.GetDevConfig(loginHandle, channel, command, config)) {
                System.out.println("通道 : " + config.nChannel);
                System.out.println("白天黑夜对应灯光配置数量:"+config.nDNLightInfoNum);
                CFG_LIGHTING_V2_DAYNIGHT[] anDNLightInfo = config.anDNLightInfo;// 白天黑夜对应灯光配置 从元素0开始分别表示 白天、夜晚、普通、顺光、一般逆光、强逆光、低照度、自定义
                /**--------白天-----------*/
                int nLightInfoLen =anDNLightInfo[0].nLightInfoLen;
                System.out.println("灯光类型数量: "+nLightInfoLen);
                CFG_LIGHTING_V2_UNIT[] anLightInfo = anDNLightInfo[0].anLightInfo;// 获取白天下的补光灯配置信息
                for (int i = 0; i < nLightInfoLen; i++) {
                    System.out.println("------------------");
                    System.out.println("灯光类型:"+ EM_CFG_LC_LIGHT_TYPE.getNoteByValue(anLightInfo[i].emLightType));
                    System.out.println("灯光模式:"+EM_CFG_LC_MODE.getNoteByValue(anLightInfo[i].emMode));
                    System.out.println("近光灯组数量 :"+anLightInfo[i].nNearLightLen);
                    NET_LIGHT_INFO[] anNearLight = anLightInfo[i].anNearLight;
                    for (int j = 0; j < anLightInfo[i].nNearLightLen; j++) {
                        System.out.println("亮度百分比:"+anNearLight[j].nLight);
                    }
                }
                // 设置，在获取配置的基础上设置
                // 配置文件 -白天    配置文件 - 白光模式   模式-手动   模式- 0-100
                config.anDNLightInfo[0].anLightInfo[1].anNearLight[0].nLight = 0;
                if (ToolKits.SetDevConfig(loginHandle, channel, command, config)) {
                    System.out.println("Set CFG_CMD_LIGHTING_V2 Succeed!");
                } else {
                    System.err.println("Set CFG_CMD_LIGHTING_V2 Failed!" + netsdk.CLIENT_GetLastError());
                }
            } else {
                System.err.println("Get CFG_CMD_LIGHTING_V2 Failed!" + netsdk.CLIENT_GetLastError());
            }
        }
    }


    /**
     * 巡航线下的预置点, param4对应结构体 NET_IN_MOVE_DIRECTLY_INFO
     */
    public void setPreset() {

        int dwPTZCommand = NetSDKLib.NET_PTZ_ControlType.NET_PTZ_POINT_SET_CONTROL;
        int nPresetIndex = 1; 	// 预置点值，从1开始
        String name = "Preset1";	// 预置点名称  TIPS:热成像预置点名称无法修改
        if(PTZControl(dwPTZCommand, 0, nPresetIndex, 0, 0, ToolKits.GetGBKStringToPointer(name))) {
            System.out.println("setPreset success!");
        } else {
            System.err.println("setPreset Failed!" + ToolKits.getErrorCode());
        }
    }

    public boolean PTZControl(int dwPTZCommand, int lParam1, int lParam2, int lParam3, int dwStop, Pointer param4) {
        int nQueryChannel = 0;//通道号
        return netsdk.CLIENT_DHPTZControlEx2(loginHandle, nQueryChannel, dwPTZCommand, lParam1, lParam2, lParam3, dwStop,param4);
    }


    // 配置登陆地址，端口，用户名，密码
    private String m_strIpAddr = "172.31.2.204";
    private int m_nPort = 37777;
    private String m_strUser = "admin";
    private String m_strPassword = "admin123";
    public static void main(String[] args) {
        SoftTourPointDemo demo=new SoftTourPointDemo();
        demo.initTest();
        demo.runTest();
        demo.endTest();
    }
}
